package com.own.business.system.menu.entity.dto;

import com.own.business.system.menu.entity.bo.SystemMenuBo;
import com.own.component.common.base.entity.BaseDto;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.component.common.util.list.ListUtil;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * SystemMenuDto
 *
 * @author chenxueli
 * @date 2024-03-31 09:22:58
 */
@Schema(title = "系统菜单" + ConstantBeanSuffix.DTO)
@Getter
@Setter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class SystemMenuDto extends BaseDto<SystemMenuBo> implements Serializable {

    @Schema(title = "菜单编码")
    private String code;

    @Schema(title = "菜单标题")
    private String title;

    @Schema(title = "父级菜单id")
    private Long parentId;

    @Schema(title = "菜单路径")
    private String path;

    @Schema(title = "菜单图标")
    private String icon;

    @Schema(title = "是否禁用 1=禁用 0=可用")
    private Integer isDisable;

    @Schema(title = "是否显示 1=显示 0=隐藏")
    private Integer isShow;

    @Schema(title = "权限标识")
    private String permission;

    @Schema(title = "排序")
    private Integer sortOrder;

    @Schema(title = "客户端id")
    private String clientId;

    @Schema(title = "菜单链名称")
    private String chainName;

    @Schema(title = "菜单链ids")
    private String chainIds;

    /**
     * 初始化
     */
    @Override
    protected void init() {
        super.init();
        if (sortOrder == null) {
            sortOrder = 0;
        }
        if (isDisable == null) {
            isDisable = 0;
        }
        if (isShow == null) {
            isShow = 1;
        }
        isDisable = isDisable % 2;
        isShow = isShow % 2;
    }

    /**
     * 验证数据
     */
    @Override
    public void check() {
        super.check();
        if (StringUtils.isBlank(clientId)) {
            throw new BusinessSimpleException("客户端不能为空");
        }
        if (StringUtils.isBlank(title)) {
            throw new BusinessSimpleException("菜单标题不能为空");
        }
        if (StringUtils.isBlank(code)) {
            throw new BusinessSimpleException("菜单编码不能为空");
        }
        // 菜单标题不能超过8位
        if (title.length() > 8) {
            throw new BusinessSimpleException("菜单标题长度不能超过8位");
        }
        // 菜单编码不能超过50位
        if (code.length() > 50) {
            throw new BusinessSimpleException("菜单编码长度不能超过50位");
        }
        // 客户端名称不能超过32位
        if (clientId.length() > 32) {
            throw new BusinessSimpleException("客户端名称长度不能超过32位");
        }
    }

    /**
     * 验证更新数据
     *
     * @param id 主键
     */
    public void checkUpdate(Long id) {
        check();
        if (StringUtils.isBlank(chainIds)) {
            return;
        }
        if (id.equals(parentId)) {
            throw new BusinessSimpleException("上级菜单不能选择当前菜单");
        }
        // 验证数据
        var chainIdList = ListUtil.Spilt.toLong(chainIds, ",");
        if (chainIdList == null || chainIdList.isEmpty()) {
            throw new BusinessSimpleException("菜单链不能为空");
        }
        if (chainIdList.contains(id)) {
            throw new BusinessSimpleException("菜单链中不能出现当前菜单");
        }
        // 判断两边的链长度
        var chainNameList = ListUtil.Spilt.to(chainName, ",");
        if (chainNameList == null || chainNameList.isEmpty() || chainNameList.size() != chainIdList.size()) {
            throw new BusinessSimpleException("菜单链名称不能为空");
        }
    }

    /**
     * 转业务层对象
     *
     * @param entity 原业务层对象
     * @return 业务层对象
     */
    @Override
    public SystemMenuBo toBo(SystemMenuBo entity) {
        if (entity == null) {
            entity = new SystemMenuBo();
        }
        entity.setCode(this.code);
        entity.setTitle(this.title);
        entity.setParentId(this.parentId);
        entity.setPath(this.path);
        entity.setIcon(this.icon);
        entity.setIsDisable(this.isDisable);
        entity.setIsShow(this.isShow);
        entity.setPermission(this.permission);
        entity.setSortOrder(this.sortOrder);
        entity.setClientId(this.clientId);
        entity.setChainName(this.chainName);
        entity.setChainIds(this.chainIds);
        return super.toBo(entity);
    }

}

